"""
示例16: 查询所有数据

演示如何使用get_all方法查询表中的所有数据
"""
import asyncio
import ygg_aiomysql as aiomysql

async def main():
    """
    主函数: 演示查询所有数据的完整流程
    """
    # 创建数据库连接池
    pool = await aiomysql.new_pool_env()
    
    try:
        # 创建测试表
        async with pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute("""
                    CREATE TABLE IF NOT EXISTS test_table (
                        id INT AUTO_INCREMENT PRIMARY KEY,
                        name VARCHAR(100),
                        age INT
                    )
                """)
                await conn.commit()
        
        # 插入测试数据
        test_data1 = {'name': '用户1', 'age': 25}
        test_data2 = {'name': '用户2', 'age': 30}
        await aiomysql.add(pool, 'test_table', test_data1)
        await aiomysql.add(pool, 'test_table', test_data2)
        print("已插入测试数据")
        
        # 查询所有数据
        print("正在查询所有数据...")
        results = await aiomysql.get_all(pool, 'test_table')
        
        # 打印查询结果
        print(f"共查询到{len(results)}条记录:")
        for idx, item in enumerate(results, 1):
            print(f"{idx}. ID:{item['id']}, 姓名:{item['name']}, 年龄:{item['age']}")
            
    finally:
        # 清理测试表
        async with pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute("DROP TABLE IF EXISTS test_table")
                await conn.commit()
        
        # 关闭连接池
        pool.close()
        await pool.wait_closed()

if __name__ == '__main__':
    asyncio.run(main())